package com.android.task5;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

//{
//	"count": 773840,"photos": [
//	{
//	"photo_id": 532693,
//	"photo_title": "Wheatfieldin afternoonlight",
//	"photo_url": "http://www.panoramio.com/photo/532693",
//	"photo_file_url": "http://static2.bareka.com/photos/medium/532693.jpg",
//	"longitude": 11.280727,
//	"latitude": 59.643198,
//	"width": 500,
//	"height": 333,
//	"upload_date": "22 January2007",
//	"owner_id": 39160,
//	"owner_name": "Snemann",
//	"owner_url": "http://www.panoramio.com/user/39160",
//	}, ...

public class PanoramioJSONResponse {
	private JSONObject JSONobj;
	private String data; // data to parse
	private ArrayList<PanoramioPhoto> mPanoramioPhotos;
	
	private final static String photoFileUrlTag = "photo_file_url";
	private final static String ownerNameTag = "owner_name";
	private final static String titleTag = "photo_title";
	
	public PanoramioJSONResponse(){
		this.mPanoramioPhotos = new ArrayList<PanoramioPhoto>();
	}
	
	public void parseJSONResponse(String dataToParse){
		JSONArray mJSONArray = null;
		try {
			JSONobj = new JSONObject(dataToParse);
			mJSONArray = JSONobj.getJSONArray("photos");
		} catch (JSONException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		if(mJSONArray != null){
			for(int i=0;i<mJSONArray.length();i++){
				String photoUrl = "";
				String photoTitle = "";
				String ownerName = "";
		try{
			JSONObject newJSONObject = mJSONArray.getJSONObject(i);
			
			photoUrl = newJSONObject.getString(photoFileUrlTag);
			photoTitle = newJSONObject.getString(titleTag);
			ownerName = newJSONObject.getString(ownerNameTag);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		PanoramioPhoto mPanoramioPhoto = new PanoramioPhoto(photoUrl, photoTitle, ownerName );
		mPanoramioPhotos.add(mPanoramioPhoto);
			}
		}
	}
	
	public ArrayList<PanoramioPhoto> getPanoramioPhotos(){
		return mPanoramioPhotos;
	}
}
